/*
 * @Date: 2021-09-10 08:23:58
 * @Author: Acckno1
 * @LastEditTime: 2021-09-10 08:44:25
 * @Description: 
 */
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <bitset>
#include <utility>
using namespace std;

#define mm(a, n) memset(a, n, sizeof a)
#define mk(a, b) make_pair(a, b)

const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<LL, LL> PLL;
typedef pair<int, LL> PIL;

inline void quickread() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

const int N = 1010, M = 1e4 + 10;

int e[M], ne[M], h[N], idx;
int n, m;
vector<int> d(N); // 记录每个点的度
int top[N];

void add(int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

// 拓扑排序
bool top_sort() {
    // 判断每一个开始元素是否 入度为0
    vector<int> d_back = d;
    for (int idx = 0; idx < n; idx ++ ) {
        int u = top[idx];
        if (d_back[u] == 0) {
            for (int i = h[u]; ~i; i = ne[i]) {
                int j = e[i];
                d_back[j] -- ;
            }
        } else {
            return false;
        }
    }
    return true;
}


inline void solution() {
    mm(h, -1);
    cin >> n >> m;
    for (int i = 0; i < m; i ++ ) {
        int a, b;
        cin >> a >> b;
        add(a, b);
        d[b] ++ ;
    }
    int cnt;
    cin >> cnt;
    vector<int> res;
    for (int _ = 0; _ < cnt; _ ++ ) {
        for (int i = 0; i < n; i ++ ) cin >> top[i];
        if (!top_sort()) res.push_back(_);
    }
    
    if (res.size()) {
        cout << res[0];
        for (int i = 1; i < res.size(); i ++ ) cout << " " << res[i];
        cout << endl;
    }
}

int main() {
    freopen("input.txt", "r", stdin);
    quickread();
    solution();
    return 0;
}